﻿

$$.Jsonar = new function () {
	var me = this;
	var jsonEscape = /[\x00-\x1f\x7f-\x9f\\"]/g;
	var jsonMeta = {
		'\b': '\\b',
		'\t': '\\t',
		'\n': '\\n',
		'\f': '\\f',
		'\r': '\\r',
		'"': '\\"',
		'\\': '\\\\'
	};
	function jsonStr(str) {
		if (!str.match(jsonEscape)) return "\"" + str + "\"";
		str = str.replace(jsonEscape, function (a) {
			var c = jsonMeta[a];
			if (c) return c;
			c = a.charCodeAt();
			return "\\u00" + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
		});
		return "\"" + str + "\"";
	}

	me.stringify = function (obj, deep) {/*只序列化纯数据*/
		var type = $.type(obj);
		if (type === "undefined" || type === "null") return "null";
		if (type === 'number' || type === 'boolean') return "" + obj;
		if (type === "date") return "\"" + obj + "\"";
		if (type === "string") return jsonStr(obj);
		if (deep !== 0 && !deep) deep = 0;
		var arr = [];
		if (type === "array") {
			for (var i = 0, len = obj.length; i < len; i++) { arr.push(me.stringify(obj[i], deep - 1)); }
			return "[" + arr.join(",") + "]";
		}
		if (deep < 1) return "null";
		if (type === "object") {
			for (var n in obj) { if (obj.hasOwnProperty(n)) arr.push("\"" + n + "\":" + me.stringify(obj[n], deep - 1)); }
			return "{" + arr.join(",") + "}";
		}
		return "null";
	};
	me.parse = function (text) {
		if (text === null || text === undefined || text.length < 1) return null;
		if ($.type(text) !== "string") return text;
		try {
			with (this) { eval("var _jdt=" + text); }
			return _jdt;
		}
		catch (e) { console.error(e); }
		return null;
	};
};